package com.wanke.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wanke.bean.CheckDetail;
import com.wanke.bean.ClazzDetail;
import com.wanke.bean.WrongResult;
import com.wanke.bean.WrongStu;
import com.wanke.dao.BanjiDao;
import com.wanke.dao.BaseDao;
import com.wanke.dao.CheckinCourseDao;
import com.wanke.dao.CheckinDormitoryDao;
import com.wanke.dao.CourseDao;
import com.wanke.dao.DormitoryDao;
import com.wanke.dao.StudentDao;
import com.wanke.dao.TeacherDao;
import com.wanke.entity.Banji;
import com.wanke.entity.CheckinCourse;
import com.wanke.entity.Course;
import com.wanke.entity.Student;
import com.wanke.entity.Teacher;

@Service("helloService")
@Transactional
public class HelloServiceImpl implements HelloService {
	@Resource(name = "baseDao")
	BaseDao baseDao;

	@Resource(name = "banjiDao")
	BanjiDao banjiDao;

	@Resource(name = "checkinCourseDao")
	CheckinCourseDao checkinCourseDao;

	@Resource(name = "checkinDormitoryDao")
	CheckinDormitoryDao checkinDormitoryDao;

	@Resource(name = "courseDao")
	CourseDao courseDao;

	@Resource(name = "dormitoryDao")
	DormitoryDao dormitoryDao;

	@Resource(name = "studentDao")
	StudentDao studentDao;

	@Resource(name = "teacherDao")
	TeacherDao teacherDao;

	@Override
	public void findAllStudent() {
		checkinCourseDao.delete(checkinCourseDao.findById(90));
	}

	public void del() {
		checkinCourseDao.deleteById(91);

	}

	public Student getLogin(String username, String password) {
		return studentDao.findByUserPass(username, password);
	}

	public Teacher getTLogin(String username, String password) {
		return teacherDao.findByUserPass(username, password);
	}

	public List<Banji> getBanjiByTeacherId(int id) {
		return banjiDao.getBjByTeachId(id);
	}

	public ClazzDetail getCheck(int stu_id) {
		Date date = new Date();
		String begin = "";
		ClazzDetail cd = new ClazzDetail();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		String strNow = sdf.format(date);
		String[] buf = strNow.split("-");
		int month = Integer.parseInt(buf[1]);
		int year = Integer.parseInt(buf[0]);
		int day = Integer.parseInt(buf[2]);
		int now = Integer.parseInt((buf[3] + buf[4]).replaceFirst("^0+", ""));
		// 根据当前时间判断是在那个时间段内上课
		if (now >= 800 && now <= 945) {
			begin = "1";
			cd.setCount("1-2");
		} else if (now >= 1000 && now <= 1145) {
			begin = "3";
			cd.setCount("3-4");
		} else if (now >= 1315 && now <= 1500) {
			begin = "5";
			cd.setCount("5-6");
		} else if (now >= 1515 && now <= 1700) {
			begin = "7";
			cd.setCount("7-8");
		} else if (now >= 1815 && now <= 2000) {
			begin = "9";
			cd.setCount("9-10");
		} else {
			return null;
		}
		// 今天周几
		SimpleDateFormat sdf2 = new SimpleDateFormat("EEEE");
		String zhouji = sdf2.format(date);

		// 根据登录的学生id查出该学生班级的id
		Student stu = studentDao.findById(stu_id);
		int banji_id = stu.getBanji().getId();

		// 得到当前课表

		Course cour = courseDao.clazzCheck(banji_id, begin, zhouji);
		if (cour == null) {
			return null;
		}

		// 设置cd的id 就课程的id
		cd.setId(cour.getId());

		// 判断上下学期 判断第几周
		if (month >= 3 && month <= 7) {
			cd.setYear((year - 1) + "-" + year);
			cd.setTeam("第二学期");
			System.out.println("---1----");
			cd.setWeek(((month - 3) * 30 + day) / 7 + 1);

		} else if (month == 8) {
			return null;
		} else {
			cd.setYear(year + "-" + (year + 1));
			cd.setTeam("第一学期");
			if (0 < month && month < 3) {
				System.out.println("---2----");
				System.out.println(month);
				cd.setWeek((month * 30 + day) / 7 + 1);
			} else if (month > 7) {
				System.out.println("---3----");
				cd.setWeek(((month - 9) * 30 + day) / 7 + 1);
			}
		}

		cd.setBanjiname(cour.getBanji().getName());
		cd.setClazzNname(cour.getName());
		cd.setTeacher(cour.getTeacherName());

		System.out.println(cd);
		return cd;
	}

	public List<Student> getAllStu() {
		List<Student> studs = studentDao.findAllStu();
		return studs;
	}

	public List getThisCCId(String today) {
		return checkinCourseDao.getThisCCId(today);
	}

	public void addCheck(String result, int cour_id, Date date, int stu_id, int cc_id) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(date);
		if (checkinCourseDao.thisExist(today, cour_id)) {
			CheckinCourse cc = new CheckinCourse();
			cc.setCreatedTime(date);
			cc.setResult(result);
			cc.setCourse(courseDao.findById(cour_id));
			Student s1 = studentDao.findById(stu_id);
			System.out.println(s1.getId());
			// cc.setStudents(new HashSet<Student>());
			// s1.setCheckinCourses(new HashSet<CheckinCourse>());
			cc.getStudents().add(s1);
			s1.getCheckinCourses().add(cc);

			checkinCourseDao.addCC(cc);
			studentDao.addStu(s1);
		} else {
			CheckinCourse cc = checkinCourseDao.findById(cc_id);
			System.out.println(cc.getId() + "//" + cc.getResult());
			System.out.println(result);
			cc.setResult(result);
			checkinCourseDao.updateCC(cc);
		}
	}

	public List<Course> getResult() {
		Date date = new Date();
		ClazzDetail cd;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(date);
		List<Course> ccs = checkinCourseDao.getToday(today);
		return ccs;
	}

	public ClazzDetail checkAgain(int cc_id) {
		ClazzDetail cd = new ClazzDetail();
		Course c = courseDao.findById(cc_id);
		cd.setYear(c.getYear());
		cd.setTeam(c.getTeam());
		cd.setBanjiname(c.getBanji().getName());
		cd.setClazzNname(c.getName());
		cd.setTeacher(c.getTeacherName());
		return cd;
	}

	public List showToday(int teacher_id) {
		List list = new ArrayList();
		Teacher t = teacherDao.findById(teacher_id);
		Set<Banji> banjis = t.getBanjis();
		Iterator<Banji> it = banjis.iterator();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(new Date());
		while (it.hasNext()) {
			Banji bj = it.next();
			CheckDetail chde = new CheckDetail();
			List<WrongStu> chidaos = new ArrayList<WrongStu>();
			List<WrongStu> qingjias = new ArrayList<WrongStu>();
			List<WrongStu> kuangkes = new ArrayList<WrongStu>();
			chde.setClazzName(bj.getName());

			Set<Student> stus = bj.getStudents();
			Iterator<Student> it2 = stus.iterator();
			while (it2.hasNext()) {
				Student s = it2.next();
				WrongStu ws1 = new WrongStu();
				ws1.setName(s.getName());
				ws1.setCode(s.getId());
				WrongStu ws2 = new WrongStu();
				ws2.setName(s.getName());
				ws2.setCode(s.getId());
				WrongStu ws3 = new WrongStu();
				ws3.setName(s.getName());
				ws3.setCode(s.getId());
				List<CheckinCourse> Todayccs = new ArrayList<CheckinCourse>();
				Set<CheckinCourse> ccs = s.getCheckinCourses();
				Iterator<CheckinCourse> it4 = ccs.iterator();
				while (it4.hasNext()) {
					CheckinCourse cc = it4.next();
					if (cc.getCreatedTime().toString().equals(today)) {
						System.out.println(cc.getCreatedTime());
						Todayccs.add(cc);
					}
				}
				int count1 = 0;
				int count2 = 0;
				int count3 = 0;
				Iterator<CheckinCourse> it3 = Todayccs.iterator();
				while (it3.hasNext()) {
					CheckinCourse cc = it3.next();
					String result = cc.getResult();

					if (result.equals("迟到")) {

						ws1.setCount(++count1);
						ws1.setResult(result);

					} else if (result.equals("请假")) {
						ws2.setCount(++count2);
						ws2.setResult(result);

					} else if (result.equals("旷课")) {
						ws3.setCount(++count3);
						ws3.setResult(result);

					}
				}
				if (count1 > 0) {
					chidaos.add(ws1);
				}
				if (count2 > 0) {
					qingjias.add(ws2);
				}
				if (count3 > 0) {
					kuangkes.add(ws3);
				}
			}
			chde.setChidao(chidaos);
			chde.setKuangke(kuangkes);
			chde.setQingjia(qingjias);
			list.add(chde);
		}
		return list;
	}

	public List<WrongResult> getStuChidao(int stu_id) {
		List<WrongResult> wrs = new ArrayList<WrongResult>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(new Date());
		Student s = studentDao.findById(stu_id);
		Set<CheckinCourse> ccs = s.getCheckinCourses();
		List<CheckinCourse> Todaygerenccs = new ArrayList<CheckinCourse>();
		Iterator<CheckinCourse> it4 = ccs.iterator();
		while (it4.hasNext()) {
			CheckinCourse cc = it4.next();
			if (cc.getCreatedTime().toString().equals(today)) {
				System.out.println(cc.getCreatedTime());
				Todaygerenccs.add(cc);
			}
		}
		Iterator<CheckinCourse> it = Todaygerenccs.iterator();
		while (it.hasNext()) {
			CheckinCourse cc = it.next();
			if (cc.getResult().equals("迟到")) {
				WrongResult wr = new WrongResult();
				wr.setCode(s.getId());
				wr.setBanji(s.getBanji().getName());
				wr.setClazzName(cc.getCourse().getName());
				wr.setDate(cc.getCreatedTime());
				wr.setTeacher(cc.getCourse().getTeacherName());
				wr.setJieci(cc.getCourse().getBeginOrder() + "-" + cc.getCourse().getEndOrder());
				wrs.add(wr);
			}
		}
		return wrs;
	}

	public List<WrongResult> getStuQingjia(int stu_id) {
		List<WrongResult> wrs = new ArrayList<WrongResult>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(new Date());
		Student s = studentDao.findById(stu_id);
		Set<CheckinCourse> ccs = s.getCheckinCourses();
		List<CheckinCourse> Todaygerenccs = new ArrayList<CheckinCourse>();
		Iterator<CheckinCourse> it4 = ccs.iterator();
		while (it4.hasNext()) {
			CheckinCourse cc = it4.next();
			if (cc.getCreatedTime().toString().equals(today)) {
				System.out.println(cc.getCreatedTime());
				Todaygerenccs.add(cc);
			}
		}
		Iterator<CheckinCourse> it = Todaygerenccs.iterator();
		while (it.hasNext()) {
			CheckinCourse cc = it.next();
			if (cc.getResult().equals("请假")) {
				WrongResult wr = new WrongResult();
				wr.setCode(s.getId());
				wr.setBanji(s.getBanji().getName());
				wr.setClazzName(cc.getCourse().getName());
				wr.setDate(cc.getCreatedTime());
				wr.setTeacher(cc.getCourse().getTeacherName());
				wr.setJieci(cc.getCourse().getBeginOrder() + "-" + cc.getCourse().getEndOrder());
				wrs.add(wr);
			}
		}
		return wrs;
	}

	public List<WrongResult> getStuKuangke(int stu_id) {
		List<WrongResult> wrs = new ArrayList<WrongResult>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(new Date());
		Student s = studentDao.findById(stu_id);
		Set<CheckinCourse> ccs = s.getCheckinCourses();
		List<CheckinCourse> Todaygerenccs = new ArrayList<CheckinCourse>();
		Iterator<CheckinCourse> it4 = ccs.iterator();
		while (it4.hasNext()) {
			CheckinCourse cc = it4.next();
			if (cc.getCreatedTime().toString().equals(today)) {
				System.out.println(cc.getCreatedTime());
				Todaygerenccs.add(cc);
			}
		}
		Iterator<CheckinCourse> it = Todaygerenccs.iterator();
		while (it.hasNext()) {
			CheckinCourse cc = it.next();
			if (cc.getResult().equals("旷课")) {
				WrongResult wr = new WrongResult();
				wr.setCode(s.getId());
				wr.setBanji(s.getBanji().getName());
				wr.setClazzName(cc.getCourse().getName());
				wr.setDate(cc.getCreatedTime());
				wr.setTeacher(cc.getCourse().getTeacherName());
				wr.setJieci(cc.getCourse().getBeginOrder() + "-" + cc.getCourse().getEndOrder());
				wrs.add(wr);
			}
		}
		return wrs;
	}

	public String getStuName(int stu_id) {
		Student s = studentDao.findById(stu_id);
		return s.getName();
	}

	public List<WrongResult> getChidao() {
		List<WrongResult> wrs = new ArrayList<WrongResult>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(new Date());
		List<Student> studs = studentDao.findAllStu();
		for (int i = 0; i < studs.size(); i++) {
			Student s = studs.get(i);
			Set<CheckinCourse> ccs = s.getCheckinCourses();
			List<CheckinCourse> Todaygerenccs = new ArrayList<CheckinCourse>();
			Iterator<CheckinCourse> it4 = ccs.iterator();
			while (it4.hasNext()) {
				CheckinCourse cc = it4.next();
				if (cc.getCreatedTime().toString().equals(today)) {
					System.out.println(cc.getCreatedTime());
					Todaygerenccs.add(cc);
				}
			}
			Iterator<CheckinCourse> it = Todaygerenccs.iterator();
			while (it.hasNext()) {
				CheckinCourse cc = it.next();
				if (cc.getResult().equals("迟到")) {
					WrongResult wr = new WrongResult();
					wr.setStu_name(s.getName());
					wr.setCode(s.getId());
					wr.setBanji(s.getBanji().getName());
					wr.setClazzName(cc.getCourse().getName());
					wr.setDate(cc.getCreatedTime());
					wr.setTeacher(cc.getCourse().getTeacherName());
					wr.setJieci(cc.getCourse().getBeginOrder() + "-" + cc.getCourse().getEndOrder());
					wrs.add(wr);
				}
			}
		}
		return wrs;
	}
	public List<WrongResult> getQingjia() {
		List<WrongResult> wrs = new ArrayList<WrongResult>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(new Date());
		List<Student> studs = studentDao.findAllStu();
		for (int i = 0; i < studs.size(); i++) {
			Student s = studs.get(i);
			Set<CheckinCourse> ccs = s.getCheckinCourses();
			List<CheckinCourse> Todaygerenccs = new ArrayList<CheckinCourse>();
			Iterator<CheckinCourse> it4 = ccs.iterator();
			while (it4.hasNext()) {
				CheckinCourse cc = it4.next();
				if (cc.getCreatedTime().toString().equals(today)) {
					System.out.println(cc.getCreatedTime());
					Todaygerenccs.add(cc);
				}
			}
			Iterator<CheckinCourse> it = Todaygerenccs.iterator();
			while (it.hasNext()) {
				CheckinCourse cc = it.next();
				if (cc.getResult().equals("请假")) {
					WrongResult wr = new WrongResult();
					wr.setStu_name(s.getName());
					wr.setCode(s.getId());
					wr.setBanji(s.getBanji().getName());
					wr.setClazzName(cc.getCourse().getName());
					wr.setDate(cc.getCreatedTime());
					wr.setTeacher(cc.getCourse().getTeacherName());
					wr.setJieci(cc.getCourse().getBeginOrder() + "-" + cc.getCourse().getEndOrder());
					wrs.add(wr);
				}
			}
		}
		return wrs;
	}
	
	public List<WrongResult> getKuangke() {
		List<WrongResult> wrs = new ArrayList<WrongResult>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(new Date());
		List<Student> studs = studentDao.findAllStu();
		for (int i = 0; i < studs.size(); i++) {
			Student s = studs.get(i);
			Set<CheckinCourse> ccs = s.getCheckinCourses();
			List<CheckinCourse> Todaygerenccs = new ArrayList<CheckinCourse>();
			Iterator<CheckinCourse> it4 = ccs.iterator();
			while (it4.hasNext()) {
				CheckinCourse cc = it4.next();
				if (cc.getCreatedTime().toString().equals(today)) {
					System.out.println(cc.getCreatedTime());
					Todaygerenccs.add(cc);
				}
			}
			Iterator<CheckinCourse> it = Todaygerenccs.iterator();
			while (it.hasNext()) {
				CheckinCourse cc = it.next();
				if (cc.getResult().equals("旷课")) {
					WrongResult wr = new WrongResult();
					wr.setStu_name(s.getName());
					wr.setCode(s.getId());
					wr.setBanji(s.getBanji().getName());
					wr.setClazzName(cc.getCourse().getName());
					wr.setDate(cc.getCreatedTime());
					wr.setTeacher(cc.getCourse().getTeacherName());
					wr.setJieci(cc.getCourse().getBeginOrder() + "-" + cc.getCourse().getEndOrder());
					wrs.add(wr);
				}
			}
		}
		return wrs;
	}
	
	public Student showStu(int stu_id){
		return studentDao.findById(stu_id);
		
	}
}
